package tt.dz.egobus.servicemanager.core.config;

import org.apache.commons.lang3.StringUtils;
import tt.dz.egobus.api.ResponseBack;
import tt.dz.egobus.servicemanager.facade.common.CommonFacade;

/**
 * @author Anson
 * @date 17-8-10
 * @name 基于dubbo实现的运行中的配置信息
 * @since 1.0.6
 */
public class DubboFacadeConvertConfigAccess extends StringConvertConfigAccess {
    private final CommonFacade commonFacade;

    public DubboFacadeConvertConfigAccess(CommonFacade commonFacade) {
        this.commonFacade = commonFacade;
    }

    @Override
    protected String tryGetValue(String fullKey, RuntimeConfigKeyEnum runtimeConfigKeyEnum) {
        if (StringUtils.isBlank(fullKey) || runtimeConfigKeyEnum == null) {
            return null;
        }
        ConfigAccessType accessType = runtimeConfigKeyEnum.getType();
        String remoteValue = null;
        ResponseBack<String> remoteValueResponse = null;
        try {
            switch (accessType) {
                case SYSTEM:
                    remoteValueResponse = commonFacade.getConfig(fullKey);
                    break;
                case OPERATION:
                    remoteValueResponse = commonFacade.getConfig(fullKey);
                    break;
                default:
            }
            if (remoteValueResponse != null && remoteValueResponse.isSuccess()
                    && !StringUtils.isBlank(remoteValueResponse.getResult())) {
                remoteValue = StringUtils.trim(remoteValueResponse.getResult());
            }
        } catch (Throwable e) {
            System.out.println(e);
        }
        return remoteValue;
    }
}
